from django.shortcuts import render
from rest_framework.views import APIView
from course import models,courseser
# Create your views here.
import json
from rest_framework.response import Response
class HomeCate(APIView):
    def get(self,request):
        allcate = models.Category.objects.filter(parent=None,flag=True).order_by("order")
        ser = courseser.CategorySerializer(allcate,many=True)
        data = ser.data
        for item in data:
            item["children"] = item['children'][:3]
        # for item in data:
        #     item["children"] = json.dumps(list(models.Category.objects.filter(parent=item["id"],flag=True).order_by("order").values("id","cname")))
        return Response({"code":200,"msg":'ok',"cate":ser.data})


# 首页展示二级分类的接口
class HomeChild(APIView):
    def get(self,request):
        # 获取到一级分类的参数
        cate = request.query_params.get("cate","")
        if not cate:
            return Response({"code":400,"msg":"参数错误"})
        # 获取二级分类
        catelist = models.Category.objects.get(id=cate,flag=True)
        ser = courseser.CategorySerializer(catelist)
        data = ser.data
        data["course"] = []
        return Response({"code":200,"msg":'ok',"cate":data})